﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//  ReadOnlyChannelControlWindow.cs
//    
// Abstract:
//
//  Code-beside for the Read-Only Channel Control Window.
//  
//--

using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;

namespace Microsoft.EnhancedReminders.OutlookAddIn
{
    /// <summary>
    /// Interaction logic for ReadOnlyChannelControlWindow.xaml
    /// </summary>
    public partial class ReadOnlyChannelControlWindow : Window
    {
        /// <summary>
        /// Command for "OK" button events.
        /// </summary>
        public readonly static RoutedCommand OkCommand = new RoutedCommand();

        /// <summary>
        /// Command for "Apply" button events.
        /// </summary>
        public readonly static RoutedCommand ApplyCommand = new RoutedCommand();

        private IList<NotificationChannel> _originalChannels;
        private bool _isDirty;
        private bool _listChanged;

        /// <summary>
        /// Returns whether or not the data is dirty; this determines whether the apply button
        /// should be enabled or not.
        /// </summary>
        private void IsDirty(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = IsValid && _isDirty;
        }

        /// <summary>
        /// ctor.
        /// </summary>
        public ReadOnlyChannelControlWindow(string title, string description, IList<NotificationChannel> channels)
        {
            InitializeComponent();

            Title = title;
            channelsDescription.Content = description;
            _originalChannels = channels;

            List<NotificationChannel> updatedChannels = new List<NotificationChannel>();
            foreach (NotificationChannel channel in channels)
            {
                updatedChannels.Add(channel.Copy());
            }

            readOnlyChannelControl.Initialize(updatedChannels);
            readOnlyChannelControl.PropertyChanged += ReadOnlyChannelControlPropertyChanged;
            Background = okButton.Background;
        }

        /// <summary>
        /// Returns whether or not the channel list was changed by this control.
        /// </summary>
        public bool ListChanged
        {
            get
            {
                return _listChanged;
            }
        }

        /// <summary>
        /// Called when a property changes on the ReadOnlyChannelControl. Flags the control as dirty.
        /// </summary>
        private void ReadOnlyChannelControlPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            _isDirty = true;
        }

        /// <summary>
        /// True if at least one channel in the list is enabled.
        /// </summary>
        private bool IsValid
        {
            get
            {
                Debug.Assert(readOnlyChannelControl != null, "readOnlyChannelControl");
                Debug.Assert(readOnlyChannelControl.Channels != null, "readOnlyChannelControl.Channels");

                NotificationChannelList channels = new NotificationChannelList(readOnlyChannelControl.Channels.Channels);
                return channels.IsEnabled;
            }
        }

        /// <summary>
        ///  Determines whether the Ok button should be enabled or not.
        /// </summary>
        private void OkCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            Debug.Assert(e != null, "e");

            e.CanExecute = IsValid;
        }

        /// <summary>
        /// Called when the Ok button is clicked. Applies all changes and closes the dialog.
        /// </summary>
        private void Ok(object sender, ExecutedRoutedEventArgs e)
        {
            Apply(sender, e);
            DialogResult = true;
            Close();
        }

        /// <summary>
        /// Called when the Apply button is clicked; applies changes made channels list.
        /// </summary>
        private void Apply(object sender, ExecutedRoutedEventArgs e)
        {
            _listChanged = true;
            _originalChannels.Clear();
            _isDirty = false;
            
            foreach (NotificationChannel channel in readOnlyChannelControl.Channels.Channels)
            {
                _originalChannels.Add(channel);
            }
        }
    }
}
